package com.yangguang.huahui.map;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.yangguang.huahui.app.App;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.utils.CommonUtil;
import com.yangguang.huahui.utils.LogUtil;
import com.yangguang.huahui.utils.SharedPreferenceUtil;

/**
 * Created by ${liangsz} on 2016/6/23.
 */
public class GaoLocUtils implements AMapLocationListener {
    /**
     * 开始定位
     */
    public final static int MSG_LOCATION_START = 0;
    /**
     * 定位完成
     */
    public final static int MSG_LOCATION_FINISH = 1;
    /**
     * 停止定位
     */
    public final static int MSG_LOCATION_STOP = 2;

    private AMapLocationClient locationClient = null;
    private AMapLocationClientOption locationOption = null;

    public GaoLocUtils(Context context) {
        locationClient = new AMapLocationClient(context);
        locationOption = new AMapLocationClientOption();
        // 设置定位模式为高精度模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);

        initOption();
    }

    @Override
    public void onLocationChanged(AMapLocation loc) {
        if (null != loc) {
            Message msg = mHandler.obtainMessage();
            msg.obj = loc;
            msg.what = MSG_LOCATION_FINISH;
            try {
                mHandler.sendMessage(msg);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_LOCATION_START:
//                    LogUtils.e("开始定位");
                    break;
                case MSG_LOCATION_FINISH:
                    LogUtil.e("完成定位",true);
                    AMapLocation loc = (AMapLocation) msg.obj;
                    if (loc.getAddress().contains("海南省")){
                        String city = loc.getAddress().substring(0, 6).replace("海南省", "");
                        if (city.contains("市"))
                            city = city.replace("市", "");
                        if (city.contains("县"))
                            city = city.replace("县", "");
                        if (city.contains("黎"))
                            city = city.replace("黎", "");
                        DataInfo.GEO_CITY = city;
                    }else if (loc.getCity().contains("市")) {
                        DataInfo.GEO_CITY = loc.getCity().replace("市", "");
                    }
                    LogUtil.e(loc.getAddress(), true);
                    LogUtil.e(DataInfo.GEO_CITY, true);
                    if (loc.getLatitude() != 0.0 && loc.getLongitude() != 0.0) {
                        DataInfo.LAT = loc.getLatitude();
                        DataInfo.LNG = loc.getLongitude();
                    }
                    if (!CommonUtil.isNull(loc.getAddress())) {
                        DataInfo.LOC_ADDRESS = loc.getAddress();
                    }
                    LogUtil.e(loc.getLatitude() + "," +loc.getLongitude(), true);
                    locationClient.stopLocation();
                    break;
                case MSG_LOCATION_STOP:
                    LogUtil.e("停止定位", true);
                    break;
            }
        }
    };

    // 根据控件的选择，重新设置定位参数
    private void initOption() {
        // 设置是否需要显示地址信息
        locationOption.setNeedAddress(true);
        /**
         * 设置是否优先返回GPS定位结果，如果30秒内GPS没有返回定位结果则进行网络定位
         * 注意：只有在高精度模式下的单次定位有效，其他方式无效
         */
//        locationOption.setGpsFirst(true);
        // 设置是否开启缓存
        locationOption.setLocationCacheEnable(true);
        // 设置发送定位请求的时间间隔,最小值为1000，如果小于1000，按照1000算
        locationOption.setInterval(5000);
       // 设置定位参数
        locationClient.setLocationOption(locationOption);

        // 启动定位
        locationClient.startLocation();
        // 设置定位监听
        locationClient.setLocationListener(this);
        mHandler.sendEmptyMessage(MSG_LOCATION_START);
    }

    public void stopLoc(){
        if (null != locationClient) {
            locationClient.stopLocation();
        }
    }

    public void onDestroy() {
        if (null != locationClient) {
            /**
             * 如果AMapLocationClient是在当前Activity实例化的，
             * 在Activity的onDestroy中一定要执行AMapLocationClient的onDestroy
             */
            locationClient.onDestroy();
            locationClient = null;
            locationOption = null;
        }
    }
    public boolean isLocation(){
        if (null != locationClient) {
            locationClient.isStarted();
            locationClient.startLocation();
            return true;
        }
        return false;
    }
}
